<?php

namespace app\crontab\controller;

use app\common\helpers\RedisUtils;
use Bl\Tools\CacheTool;
use fast\Date;

/**
 * 每小时任务
 * @package app\crontab\controller
 */
class Everyhour extends BaseCrontab
{

    public $lockPrefix;

    public function _initialize()
    {
        parent::_initialize();
        $this->lockPrefix = date('YmdH', $this->time) . ":crontab:hour:";
    }

    /**
     * 分布式锁
     * @param $key
     * @return bool
     */
    public function lock($key): bool
    {
        return RedisUtils::lock($this->lockPrefix . $key, Date::HOUR);

    }

    /**
     * 每小时定时运行
     * php public/index.php /crontab/Everyhour
     */
    public function index()
    {
        parent::index();
    }

    /**
     * demo
     * php public/index.php /crontab/Everyhour/demo
     * @execute true
     */
    public function demo()
    {

        if ($this->lock(__FUNCTION__)) {
            $this->show_msg(__FUNCTION__ . " repeat");
            return true;
        }

        $this->show_msg('demo end');
    }

}
